package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcNotices;
import com.cloud.bc.mapper.BcNoticesMapper;
import com.cloud.bc.model.request.ChatRequest;
import com.cloud.bc.service.BcNoticesService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 通知
 *
 * @author douhaichao code generator
 * @date 2021-01-12 14:15:30
 */
@Service
@AllArgsConstructor
public class BcNoticesServiceImpl extends ServiceImpl<BcNoticesMapper, BcNotices> implements BcNoticesService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcNotices selectOne(BcNotices req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcNotices>();
        queryWrapper.setEntity(req);
        queryWrapper.orderByDesc("id");
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcNotices> selectAll(BcNotices req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcNotices> selectPage(Page page, BcNotices req) {
        QueryWrapper<BcNotices> bcNoticesQueryWrapper = new QueryWrapper<>();
        bcNoticesQueryWrapper.setEntity(req);
        bcNoticesQueryWrapper.orderByDesc("id");
        IPage<BcNotices> list = baseMapper.selectPage(
                page, bcNoticesQueryWrapper);
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcNotices> selectPageByLike(Page page, BcNotices req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getTargetId()) {
            queryWrapper.eq("target_id", req.getTargetId());
        }

        if (!StrUtil.isEmpty(req.getUidpid())) {
            queryWrapper.like("uidpid", req.getUidpid());
        }

        if (null != req.getWordType()) {
            queryWrapper.eq("word_type", req.getWordType());
        }

        if (!StrUtil.isEmpty(req.getWords())) {
            queryWrapper.like("words", req.getWords());
        }

        if (null != req.getType()) {
            queryWrapper.eq("type", req.getType());
        }

        if (null != req.getIsLook()) {
            queryWrapper.eq("is_look", req.getIsLook());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getUpdateTime()) {
            queryWrapper.eq("update_time", req.getUpdateTime());
        }
        IPage<BcNotices> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcNotices req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BcNotices> selectNoticeList(ChatRequest req) {
        List<BcNotices> bcNotices = baseMapper.selectNoticeList(req);
//查询出未读ID，变更为已读
        Set<BcNotices> collect = bcNotices.stream().filter(t -> t.getIsLook().equals(0)).collect(Collectors.toSet());
        Set<Integer> readList = collect.stream().map(t -> t.getId()).collect(Collectors.toSet());
        if (null != readList && readList.size() > 0) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id", readList);
            BcNotices bcNoticesRead = new BcNotices();
            bcNoticesRead.setIsLook(1);
            baseMapper.update(bcNoticesRead, queryWrapper);
        }
        return bcNotices;
    }
}
